const express = require('express')
const router = express.Router()

const { 
    getOfficialStaffList,
    pagingOfficialStaffList,
    searchOfficialStaffName ,
    deleteOfficialStaff,
    batchDeleteOfficialStaff,
    addOfficialStaff,
    updateOfficialStaff,
    searchEntryStaffName,
    pageSearchEntryStaffName,
} = require('../db/model/StaffModel/officialStaffModel')

const {
    getTemporaryStaffList,
    pageGetTemporaryStaffList,
    searchTemporaryStaffName,
    batchDeleteTemporaryStaff,
    addTemporaryStaff,
    updateTemporaryStaff,
    becomeOfficialStaff,
    searchEntryStaff,
    pageSearchEntryStaff
} = require('../db/model/StaffModel/temporaryStaffModel')

// 正式员工的接口

// 获取正式员工列表
/*  get请求
 *  无参数
*/
router.get('/getOfficialStaffList',(req,res) => {
    const result = getOfficialStaffList()
    return result.then(val => {
        if(val.length !== 0){
            res.send({code:200,msg:'查询成功',officialStaffList:val})
        }else{
            res.send({code:-1,msg:'查询失败'})
        }
    })
})

// 分页查询正式员工
/**
 * post请求
 * 参数：page页码，pageSize每页数据条数
 */
router.post('/pagingOfficialStaffList',(req,res) => {
    let {page, pageSize} = req.body
    let mess = {
        code:200,
        msg:'查询成功',
        page: page,
        pageSize,pageSize
    }
    const result = pagingOfficialStaffList(page,pageSize)
    return result.then(val => {
        if(val.length !== 0){
            mess.officialStaffList = val
            res.send(mess)
        }else{
            res.send({code:-1,msg:'查询失败',officialStaffList:[]})
        }
    })
})

// 模糊查询
/* post请求
 * 参数为员工名字或员工名字中的某个字
*/
router.post('/searchOfficialStaffName',(req,res) => {
    let staffName = req.body.name
    const result = searchOfficialStaffName(staffName)
    return result.then(val => {
        if(val.length !== 0){
            res.send({code:200,msg:'查询成功',officialStaffNameList:val})
        }else{
            res.send({code:-1,msg:'查询失败',officialStaffNameList:val})
        }
    })
})

// 分页查询入职时间
/**
 * 分页查询入职时间
 * post请求
 * 参数：起始入职时间，截止入职时间，page，pageSize
 */
router.post('/searchEntryStaffName',(req,res) => {
    let { startTime, endTime } = req.body
    const result = searchEntryStaffName(startTime,endTime)
    return result.then(val => {
        if(val.length !== 0){
            res.send({code:200,msg:'查询成功',officialStaffNameList:val})
        }else{
            res.send({code:-1,msg:'查询失败',officialStaffNameList:val})
        }
    })
})

// 分页查询入职时间
/**
 * 分页查询入职时间
 * post请求
 * 参数：起始入职时间，截止入职时间，page，pageSize
 */
router.post('/pageSearchEntryStaffName',(req,res) => {
    let { startTime, endTime, page, pageSize } = req.body
    const result = pageSearchEntryStaffName(startTime,endTime,page,pageSize)
    return result.then(val => {
        if(val.length !== 0){
            res.send({code:200,msg:'查询成功',officialStaffNameList:val})
        }else{
            res.send({code:-1,msg:'查询失败',officialStaffNameList:val})
        }
    })
})

//删除
/**
 * 删除
 * get请求
 * 参数为员工id
 */
router.get('/delOfficialStaff',(req,res) => {
    let id = req.query.id
    const result = deleteOfficialStaff(id)
    return result.then(val => {
        if(val){
            res.send({code:200,msg:'删除成功'})
        }else{
            res.send({code:-1,msg:'删除失败'})
        }
    })
})

//批量删除
/**
 * post请求
 * 参数：批量删除数据id的数组
 */
router.post('/batchDelOfficialStaff',(req,res) => {
    let idAry = req.body.idAry
    const result = batchDeleteOfficialStaff(idAry)
    return result.then(val => {
        if(val){
            res.send({code:200,msg:'删除成功'})
        }else{
            res.send({code:-1,msg:'删除失败'})
        }
    })
})

// 新增
/**
 * post请求
 * 参数：name,age,gender,phone,address,duty,salary
 * entryTime后端自动拼接
 */
router.post('/addOfficialStaff', (req,res) => {
    let { name,age,gender,phone,address,duty,salary } = req.body
    const result = addOfficialStaff(name,age,gender,phone,address,duty,salary)
    return result.then(val => {
        if(val){
            res.send({code:200,msg:'添加成功'})
        }else{
            res.send({code:-1,msg:'添加失败'})
        }
    })
})

// 修改
/**
 * post请求
 * 参数：id,name,age,gender,phone,address,duty,salary
 */
router.post('/updateOfficialStaff',(req,res) => {
    let { id,name,age,gender,phone,address,duty,salary } = req.body
    const result = updateOfficialStaff(id,name,age,gender,phone,address,duty,salary)
    return result.then(val => {
        if(val){
            res.send({code:200,msg:'修改成功'})
        }else{
            res.send({code:-1,msg:'修改失败'})
        }
    })
})






// 临时员工接口

/**
 * 获取临时员工列表
 * get请求
 * 无参数
 */
router.get('/getTemporaryStaffList',(req,res) => {
    const result = getTemporaryStaffList()
    return result.then(val => {
        if(val.length !== 0){
            res.send({code:200,msg:'查询成功',temporaryStaffList:val})
        }else{
            res.send({code:-1,msg:'查询失败'})
        }
    })
})

/**
 *  分页查询所有临时员工列表
 * post
 * 参数：page，pageSize
 */
router.post('/pageGetTemporaryStaffList',(req,res)=>{
    let {page, pageSize} = req.body
    let mess = {
        code:200,
        msg:'查询成功',
        page: page,
        pageSize,pageSize
    }
    const result = pageGetTemporaryStaffList(page,pageSize)
    return result.then(val => {
        if(val.length !== 0){
            mess.temporaryStaffList = val
            res.send(mess)
        }else{
            res.send({code:-1,msg:'查询失败',temporaryStaffList:[]})
        }
    })
})


// 模糊查询
/* post请求
 * 参数为员工名字或员工名字中的某个字
*/
router.post('/searchTemporaryStaffName',(req,res) => {
    let staffName = req.body.name
    const result = searchTemporaryStaffName(staffName)
    return result.then(val => {
        if(val.length !== 0){
            res.send({code:200,msg:'查询成功',temporaryStaffNameList:val})
        }else{
            res.send({code:-1,msg:'查询失败',temporaryStaffNameList:val})
        }
    })
})


/**
 * 查询所有符合入职条件的临时员工
 * post请求
 * 参数为：入职时间
 */
router.post('/searchEntryStaff',(req,res)=> {
    let { time } = req.body
    const result = searchEntryStaff(time)
    return result.then(val => {
        if(val.length !== 0){
            res.send({code:200,msg:'查询成功',temporaryStaffNameList:val})
        }else{
            res.send({code:-1,msg:'查询失败',temporaryStaffNameList:val})
        }
    })
})

/**
 * 分页查询所有符合入职条件的临时员工
 * post请求
 * 参数为：入职时间,page,pageSize
 */
router.post('/pageSearchEntryStaff',(req,res)=>{
    let { time,page,pageSize } = req.body
    const result = pageSearchEntryStaff(time,page,pageSize)
    return result.then(val => {
        if(val.length !== 0){
            res.send({code:200,msg:'查询成功',temporaryStaffNameList:val})
        }else{
            res.send({code:-1,msg:'查询失败',temporaryStaffNameList:val})
        }
    })
})

// 转正
/**
 * post请求
 * 参数：参数为实习员工id,转正后的name,age,gender,phone,address,duty,salary
 */
router.post('/becomeOfficialStaff',(req,res) => {
    let { id,name,age,gender,phone,address,salary } = req.body
    const result = becomeOfficialStaff(id,name,age,gender,phone,address,salary)
    return result.then(val => {
        console.log(val)
        if(val){
            res.send({code:200,msg:'转正成功'})
        }else{
            res.send({code:-1,msg:'转正失败'})
        }
    })
})

//批量删除
/**
 * post请求
 * 参数：批量删除数据id的数组
 */
router.post('/batchDeleteTemporaryStaff',(req,res) => {
    let idAry = req.body.idAry
    const result = batchDeleteTemporaryStaff(idAry)
    return result.then(val => {
        if(val){
            res.send({code:200,msg:'删除成功'})
        }else{
            res.send({code:-1,msg:'删除失败'})
        }
    })
})

// 新增
/**
 * post请求
 * 参数：name,age,gender,phone,address,duty,salary,trialDay
 * entryTime后端自动拼接
 */
router.post('/addTemporaryStaff', (req,res) => {
    let { name,age,gender,phone,address,salary,trialDay } = req.body
    const result = addTemporaryStaff(name,age,gender,phone,address,salary,trialDay)
    return result.then(val => {
        if(val){
            res.send({code:200,msg:'新增成功'})
        }else{
            res.send({code:-1,msg:'新增失败'})
        }
    })
})

// 修改
/**
 * post请求
 * 参数：id,name,age,gender,phone,address,salary,trialDay
 */
router.post('/updateTemporaryStaff',(req,res) => {
    let { id,name,age,gender,phone,address,salary,trialDay } = req.body
    const result = updateTemporaryStaff(id,name,age,gender,phone,address,salary,trialDay)
    return result.then(val => {
        if(val){
            res.send({code:200,msg:'修改成功'})
        }else{
            res.send({code:-1,msg:'修改失败'})
        }
    })
})

module.exports = router